package action;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;

import entity.Students;
import service.StudentsDAO;
import service.impl.StudentsDAOImpl;

public class StudentsAction extends SuperAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	public String queryAll(){
		
		StudentsDAO studentsDAO = new StudentsDAOImpl();
		List<Students> list = studentsDAO.queryAllStudents();
		if (list!=null && list.size()>0) {
			httpSession.setAttribute("students_list", list);
		}
		return "query_success";
	}
	
	public String deleteBySid(){
		
		StudentsDAO studentsDAO = new StudentsDAOImpl();
		String sid = request.getParameter("sid");
		studentsDAO.deleteStudents(sid);
		return "delete_success";
	
	}
	
	public String add() throws ParseException{
		
		StudentsDAO studentsDAO = new StudentsDAOImpl();
		Students students = new Students();
		students.setSname(request.getParameter("sname"));
		students.setAddress(request.getParameter("address"));
		students.setGender(request.getParameter("gender"));
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		students.setBirthday(sdf.parse(request.getParameter("birthday")));
		studentsDAO.addStudents(students);
		
		return "add_success";
	}
	
	public String modify(){
		//获得传递过来的学生编号
		String sid = request.getParameter("sid");
		StudentsDAO studentsDAO = new StudentsDAOImpl();
		Students students = studentsDAO.queryStudentsBySid(sid);
		//保存在会话中
		httpSession.setAttribute("modify_students", students);
		return "modify";
	}
	
	public String save() throws Exception{
		
		StudentsDAO studentsDAO = new StudentsDAOImpl();
		Students students = new Students();
		
		students.setSid(request.getParameter("sid"));
		students.setSname(request.getParameter("sname"));
		students.setAddress(request.getParameter("address"));
		students.setGender(request.getParameter("gender"));
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		students.setBirthday(sdf.parse(request.getParameter("birthday")));
		
		studentsDAO.updateStudents(students);
		
		return "save_success";
	}

}
